package com.zfy.tingshu.search.api;

import com.zfy.tingshu.common.result.Result;
import com.zfy.tingshu.model.search.AlbumInfoIndex;
import com.zfy.tingshu.query.search.AlbumIndexQuery;
import com.zfy.tingshu.search.service.SearchService;
import com.zfy.tingshu.vo.search.AlbumSearchResponseVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import cn.hutool.json.JSONObject;

import jakarta.json.JsonObject;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Tag(name = "搜索专辑管理")
@RestController
@RequestMapping("api/search/albumInfo")
@SuppressWarnings({"all"})
public class SearchApiController {

    @Autowired
    private SearchService searchService;


    @Operation(summary = "获取排行榜")
    @GetMapping("/findRankingList/{category1Id}/{rangking}")
    public Result findRankingList(@PathVariable Long category1Id,@PathVariable String rangking){
        //调用服务层方法
       List<AlbumInfoIndex> albumInfoIndexList = searchService.findRankingList(category1Id,rangking);
        //默认返回
        return Result.ok(albumInfoIndexList);
    }

    @SneakyThrows
    @Operation(summary = "更新排行榜")
    @GetMapping("updateLatelyAlbumRanking")
    public Result updateLatelyAlbumRanking(){
        //调用服务层方法
        searchService.updateLatelyAlbumRanking();
        return Result.ok();
    }

    @Operation(summary = "获取频道页数据")
    @GetMapping("channel/{category1Id}")
    public Result channel(@PathVariable Long category1Id){
        //调用服务层方法
        List<Map<String,Object>> mapList = null;

        mapList=searchService.channel(category1Id);

        return Result.ok(mapList);
    }


    @Operation(summary = "批量上架")
    @GetMapping("batchUpperAlbum")
    public Result batchUpperAlbum(){
//        long l = System.currentTimeMillis();
        //循环
        for (long i =1;i<=1500;i++){
            searchService.upperAlbum(i);
        }

        //返回数据
        return Result.ok();
    }

    @Operation(summary = "上架专辑")
    @GetMapping("upperAlbum/{albumId}")
    public Result upperAlbum(@PathVariable Long albumId){
        searchService.upperAlbum(albumId);
        return Result.ok();
    }

    @Operation(summary = "下架专辑")
    @GetMapping("lowerAlbum/{albumId}")
    public Result lowerAlbum(@PathVariable Long albumId){
        searchService.lowerAlbum(albumId);
        return Result.ok();
    }

    @Operation(summary = "搜索专辑")
    @PostMapping
    public Result search(@RequestBody AlbumIndexQuery albumIndexQuery){
        AlbumSearchResponseVo albumSearchResponseVo = searchService.search(albumIndexQuery);
        return Result.ok(albumSearchResponseVo);
    }
    @Operation(summary = "关键字自动补全")
    @GetMapping("completeSuggest/{keyword}")
    public Result<List<String>> completeSuggest(@PathVariable String keyword){
        //根据关键词查询补全
        return Result.ok(searchService.completeSuggest(keyword));
    }



}

